package com.example.com.skills_utf8;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.app.BaseFragment;
import com.app.net.NetHelper;
import com.app.util.Utils;
import com.app.util.Utils_Dialog;
import com.app.util.Utils_Log;
import com.baidu.location.BDLocation;
import com.utils.g;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

/**
 * Created by matengfei on 15/7/27.
 */
public class _62RegisterLocationFragment extends BaseFragment{


    TextView tv_lo_la;
    Button btn_submit;
    WebView webview;

    BDLocation bd;
    int type = 2;



    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            bd = (BDLocation)msg.obj;
            tv_lo_la.setText("" + bd.getLongitude() + "," + bd.getLatitude());

            ApplicationCustom.getInstance().location.OnStop();

            String url = g.SERVER_PAY_HOST + g.SERVER_PAY_API.baidumapdisplay;
            url +="?lo="+bd.getLongitude() + "&la="+bd.getLatitude();
            Utils_Log.e(TAG, "url: "+url);
            webview.loadUrl(url);
        }
    };


    @Override
    public Integer InitLayout() {
        return R.layout.fragment__62_register_location;
    }

    @Override
    public void InitView(View view) {

        webview = (WebView)view.findViewById(R.id.webview);
        tv_lo_la = (TextView)view.findViewById(R.id.tv_lo_la);
        btn_submit = (Button)view.findViewById(R.id.btn_submit);


    }

    @Override
    public void InitListener() {

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                do_location();
            }
        });



    }

    @Override
    public void InitProcess() {

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);



        ApplicationCustom.getInstance().location.SetHandler(handler, false);

    }


    void do_location(){

        if(bd != null)
            requestNetData_location("" + bd.getLatitude(), "" + bd.getLongitude(), "" + bd.getRadius());


    }


    void requestNetData_location(final String Latitude, final String Longitude, final String Radius){


        Utils.showLoadingDialog(activity, "", false);


        new NetHelper(mRequestQueue){

            @Override
            public String init(ArrayList<BasicNameValuePair> map) {


                map.add(new BasicNameValuePair("id_umeng", g.id_umeng));
                map.add(new BasicNameValuePair("id_user", g.id_user ));
                map.add(new BasicNameValuePair("type", ""+type));
                map.add(new BasicNameValuePair("Latitude", Latitude));
                map.add(new BasicNameValuePair("Longitude", Longitude));
                map.add(new BasicNameValuePair("Radius", Radius));

                return g.SERVER_PAY_HOST + g.SERVER_PAY_API.register;
            }

            @Override
            public void onComplete(String message) throws JSONException {

                JSONObject obj = new JSONObject(message);
                JSONObject data = obj.getJSONObject("data");
                String msg = obj.getString("message");
                int result = obj.getInt("result");
                if(result == 0){

                    Utils_Dialog.ShowTips(activity, msg, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                            Intent intent = new Intent(activity, _62LoginActivity.class);
                            startActivity(intent);
                            activity.finish();

                        }
                    }, null);


                }
                else{
                    Utils_Dialog.ShowTips(activity, msg, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {


                        }
                    }, null);

                }

                //ShowToast( msg );

            }

            @Override
            public void OnFinal() {
                super.OnFinal();

                Utils.dismissLoadingDialog();
            }

        }.start_volley_GET();
    }




}

